package com.zline.foodraise.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.zline.foodraise.R;
import com.zline.foodraise.adapter.ChooseProvinceAdapter;
import com.zline.foodraise.constant.Constant;
import com.zline.foodraise.db.DatabaseHelper;
import com.zline.foodraise.db.dao.ProvinceDao;
import com.zline.foodraise.entity.Province;
import com.zline.foodraise.listener.RecyclerItemClicked;
import com.zline.foodraise.util.LUtil;
import com.zline.foodraise.view.DrawableDividerItemDecoration;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;

import java.util.List;

/**
 * Created by heshantao on 15/7/13.
 * 选择所在地(省份)
 */
@EActivity(R.layout.activity_choose_location)
public class ChooseProvinceActivity extends ZlineActivity implements RecyclerItemClicked {
    private static final String LOG_TAG = ChooseProvinceActivity.class.getSimpleName();
    //标题
    @StringRes(R.string.choose_location)
    String title;
    @ViewById(R.id.app_common_header_title_tv)
    TextView title_tv;
    @ViewById(R.id.choose_location_recyclerView)
    RecyclerView province_recyclerView;
    @Bean
    ChooseProvinceAdapter adapter;
    SparseArray<Province> provinceSparseArray;
    DatabaseHelper helper;
    @Bean
    ProvinceDao provinceDao;

    @AfterViews
    void doSomeAfterViews() {
        initData();
        initView();
        setRecyclerView();
    }

    void initView() {
        title_tv.setText(title);
    }

    void initData() {
        getProvinces();
    }

    //获得省份列表
    void getProvinces() {
        getHelper();
        provinceDao.setHelper(helper);
        List<Province> list = provinceDao.findAll();
        provinceSparseArray = new SparseArray<Province>();
        for (int i = 0; i < list.size(); i++) {
            provinceSparseArray.put(i, list.get(i));
        }
    }

    //设置recyclerView
    void setRecyclerView() {
        province_recyclerView.addItemDecoration(new DrawableDividerItemDecoration(getResources().getDrawable(R.drawable.recycleview_divider), true, true));
        province_recyclerView.setHasFixedSize(true);
        province_recyclerView.setOverScrollMode(RecyclerView.OVER_SCROLL_NEVER);//去除阴影
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        province_recyclerView.setLayoutManager(layoutManager);
        adapter.setAdapterData(provinceSparseArray);
        adapter.setRecycleItemClickListener(this);
        province_recyclerView.setAdapter(adapter);
    }


    //slite 实例
    void getHelper() {
        if (helper == null) {
            helper = OpenHelperManager.getHelper(ChooseProvinceActivity.this, DatabaseHelper.class);
        }
    }


    @Click(R.id.app_common_header_left_icon_iv)
    void onClick(View view) {
        goback();
    }


    @OnActivityResult(Constant.PROVINCE_CHOOSE_REQUEST_CODE)
    void onActivityResult(int resultCode, Intent data, @OnActivityResult.Extra String value) {
        switch (resultCode) {
            case Constant.CITY_CHOOSE_RESULT_CODE:
                setChoseProvinceResult(value);
                LUtil.i(LOG_TAG, "________选择城市__________");
                break;
            default:
                break;
        }
    }


    void  setChoseProvinceResult(String cityName){
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString(Constant.VALUE, cityName);
        intent.putExtras(bundle);
        setResult(Constant.PROVINCE_CHOOSE_RESULT_CODE, intent);
        finish();
    }

    @Override
    public void onRecyclerItemClicked(int position) {
        // LUtil.i(LOG_TAG, "_________第几个省会_______" + position);
        if (provinceSparseArray != null) {
            int provinceId = provinceSparseArray.get(position).getProvinceId();
            ChooseCityActivity_.intent(ChooseProvinceActivity.this)
                    .provinceId(provinceId).startForResult(Constant.PROVINCE_CHOOSE_REQUEST_CODE);
        }

    }

    @Override
    public void onRecyclerItemClicked(int flag, int position) {

    }

    @Override
    public void onRecyclerItemClicked(int flag, int position, int parent) {

    }

    @Override
    public void onRecyclerClickedDelete(int position) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (helper != null) {
            OpenHelperManager.releaseHelper();
            helper = null;
        }
    }
}
